package com.navicoa.e_libreria.presentacion;

import com.navicoa.e_libreria.utiles.AsistenteDeConexion;
import com.vaadin.Application;
import com.vaadin.terminal.Resource;
import com.vaadin.terminal.Sizeable;
import com.vaadin.terminal.ThemeResource;
import com.vaadin.ui.AbsoluteLayout;
import com.vaadin.ui.Component;
import com.vaadin.ui.ComponentContainer;
import com.vaadin.ui.Embedded;
import com.vaadin.ui.HorizontalLayout;
import com.vaadin.ui.Label;
import com.vaadin.ui.Panel;
import com.vaadin.ui.VerticalLayout;
import com.vaadin.ui.VerticalSplitPanel;
import com.vaadin.ui.Window;
import com.vaadin.ui.themes.Reindeer;

public class InicioAplicacion extends Application
{
	private Class<?> claseDestino = VistaLogin.class;
	
	private VerticalSplitPanel tapiz = new VerticalSplitPanel();
	
	private static boolean booleanEstado = false;
	
	private final String etiquetaConexion = "Base de datos:";
	private final String etiquetaPanel = "Estado del sistema";
	
	private final static ThemeResource ok = new ThemeResource("icons/16/ok.gif");
	private final static ThemeResource ko = new ThemeResource("icons/16/cancel.png");

	private static Embedded eI = new Embedded(null, ko);
	
	public static void conexion(boolean b){
		Resource icono;
		
		if(booleanEstado != b){
			if(b){
				icono = ok;
				booleanEstado = true;
			}else{
				icono = ko;
				booleanEstado = false;				
			}
			
			eI.setSource(icono);
		}
	}
	
    /**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	@Override
    public void init()
    {
        setMainWindow(new Window("E-Libreria"));
        setTheme("e_libreria");
        
        //Iniciar conexion a base de datos
        AsistenteDeConexion.lanzar();
        
        tapiz.setSplitPosition(80, Sizeable.UNITS_PIXELS, true);
        tapiz.setLocked(true);
        tapiz.setMargin(true);
        
        Panel pie = new Panel(etiquetaPanel);
		pie.setStyleName(Reindeer.PANEL_LIGHT);

        VerticalLayout layout = (VerticalLayout) pie.getContent();
        layout.setMargin(true); 
        layout.setSpacing(true); 
				
		pie.setHeight("100%");
		pie.setWidth("100%");
		
		HorizontalLayout h = new HorizontalLayout();
		
		Label lConexion = new Label(etiquetaConexion);
		
		h.setSpacing(true);
		
		h.addComponent(lConexion);
		h.addComponent(eI);
		
		pie.addComponent(h);
		
		tapiz.setSecondComponent(pie);
        
        //Inicio:
        try {
		setMain((AbsoluteLayout) claseDestino.newInstance());
		} catch (InstantiationException e) {
			e.printStackTrace();
		} catch (IllegalAccessException e) {
			e.printStackTrace();
		}
        
        getMainWindow().setContent(tapiz);
    }
	
	public void setMain(ComponentContainer c){
		tapiz.setFirstComponent(c);		
	}
    
	public void setSeccion(ComponentContainer c){
		Component fTapiz = tapiz.getFirstComponent();
				
		if(fTapiz instanceof VistaMain){
			//Estamos en el Main
			VerticalSplitPanel vfTapiz = ((VistaMain) fTapiz).getTapiz();
			vfTapiz.setSecondComponent(c);
		}
	}
}
